package com.xo.xodemo.controller.threesixfivepush;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xo.xodemo.common.Result;
import com.xo.xodemo.mapper.db1.ThreeSixFivePushMapper;
import com.xo.xodemo.model.ThreeSixFivePushModel;
import com.xo.xodemo.springsecurityconfig.CustomUserDetails;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author nine
 * @date 2024/4/19/019
 * @Description
 */
@Controller
@RequestMapping("/pushinfo")
public class ThreeSixFivePushController {
    @Autowired
    private ThreeSixFivePushMapper threeSixFivePushMapper;

    @RequestMapping("pushinfo")
    @ResponseBody
    public Result getAllThreeSixFivePushInfo(@Param("page") Integer page, @Param("limit")Integer limit) throws IOException {
        PageInfo<ThreeSixFivePushModel> pi = new PageInfo<>(threeSixFivePushMapper.getAllData());
        return    Result.success(pi);
    }
    @RequestMapping("selectnewpushinfo")
    @ResponseBody
    public List<ThreeSixFivePushModel> getAllThreeSixFivePushInfo() throws IOException {
        return  threeSixFivePushMapper.getAllData();
    }
    @RequestMapping("mypushinfo")
    @ResponseBody
    public Result getMyThreeSixFivePushInfo(@AuthenticationPrincipal CustomUserDetails userDetails,@Param("page") Integer page, @Param("limit")Integer limit) throws IOException {
        Integer whetherThereIsPermissionOrNot = threeSixFivePushMapper.whoCanSeeEveryoneSPushMessages(userDetails.getPositionId());
        if(whetherThereIsPermissionOrNot==0){
            PageHelper.startPage(page,limit);
            PageInfo<ThreeSixFivePushModel> pi = new PageInfo<>(threeSixFivePushMapper.getMyData(userDetails.getRealName()));
            return    Result.success(pi);
        }else{
            PageHelper.startPage(page,limit);
            PageInfo<ThreeSixFivePushModel> pi = new PageInfo<>(threeSixFivePushMapper.getAllTheHistoryOfEveryone());
            return    Result.success(pi);
        }

    }
    @RequestMapping("pushinfodetailpage")
    public String getThreeSixFivePushInfodetailpage(@AuthenticationPrincipal CustomUserDetails userDetails, String id, Model model) throws ParseException {
        ThreeSixFivePushModel threeSixFivePushModel = threeSixFivePushMapper.getThreeSixFivePushModelById(id);
        String reportDateString = threeSixFivePushModel.getReportDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date reportDate = format.parse(reportDateString);
        model.addAttribute("username", userDetails.getRealName());
        model.addAttribute("id", id);
        model.addAttribute("reportDate",reportDate);
        model.addAttribute("threeSixFivePushModel",threeSixFivePushModel);
        return "pushinfo/pushinfodetailpage";
    }
    @RequestMapping("pushinfodetailpageforme")
    public String getThreeSixFivePushInfodetailpageforme(@AuthenticationPrincipal CustomUserDetails userDetails, String id, Model model) throws ParseException {
        ThreeSixFivePushModel threeSixFivePushModel = threeSixFivePushMapper.getThreeSixFivePushModelById(id);
        String reportDateString = threeSixFivePushModel.getReportDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date reportDate = format.parse(reportDateString);
        model.addAttribute("username", userDetails.getRealName());
        model.addAttribute("id", id);
        model.addAttribute("reportDate",reportDate);
        model.addAttribute("threeSixFivePushModel",threeSixFivePushModel);
        return "pushinfo/pushinfodetailpageforme";
    }
    //获取登录用户的真实姓名
    @RequestMapping("userProfile")
    @ResponseBody
    public String userProfile(@AuthenticationPrincipal CustomUserDetails userDetails) {
        return userDetails.getRealName();
    }

}
